<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/11/22
 * Time: 14:12
 */

namespace app\admin\controller;


use houdunwang\arr\Arr;
use think\Controller;


/**
 * Class Category
 * @package app\admin\controller
 * 分类管理控制器
 */
class Category extends common
{
    protected $db;
    public function __construct()
    {
        parent::__construct();
        $this->db = new \app\common\model\Category();
    }
    //分类首页
    public function index(){
        $data=$this->db->CateData();
        $this->assign('data',$data);
        //视图
        return view();
    }
    //父级分类添加
    public function add(){
        //获取类型数据库并分配数据
        $type=db('type')->select();
        //分类添加
        if(request()->isPost()){
            $data=input('post.');
           $res=$this->db->add($data);
            if($res['valid']){
                $this->success($res['msg'],'index');
            }else{
                $this->error($res['msg']);
            }
        }
        $this->assign('type',$type);
        //视图
        return view();
    }
    //子级分类添加
    public function addSon($cid){
        //获取父级分类
        $categoryData=db('category')->find($cid);
        $this->assign('categoryData',$categoryData);
        //获取类型数据
        $type=db('type')->select();
        $this->assign('type',$type);
        if(request()->isPost()){
            $data=input('post.');
            $res=$this->db->add($data);
            if($res['valid']){
                $this->success($res['msg'],'index');
            }else{
                $this->error($res['msg']);
            }
        }

        //视图
        return view();
    }
    //编辑分类数据
    public function edit(){
        //2.获取旧数据
        $cid = input('param.cid');
        $oldData = db('category')->find($cid);
        $this->assign('oldData',$oldData);
        //3.处理所属分类(三步)
        $cateData = $this->db->getCateData($cid);
        $this->assign('cateData',$cateData);
        //获取类型数据
        $type=db('type')->select();
        $this->assign('type',$type);
        //处理类型数据
        if(request()->isPost()){
            $data = input('post.');
            $res = $this->db->edit($data);
            if($res['valid']){
                $this->success($res['msg'],'index');
            }else{
                $this->error($res['msg']);
            }
        }

        //视图
        return view();
    }
    //删除
    public function del($cid){
        $res = $this->db->del($cid);
        if ($res['valid']){
            $this->success($res['msg'],'admin/category/index');
        }else{
            $this->error($res['msg']);
        }
    }
}